package com.basic.library.utils;

import android.annotation.SuppressLint;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created by yys on 2016/11/22.
 */

public class DimenTool {
    public final static int defaultDp = 375;
    static int[] dpis = {240, 320, 360, 384, 400, 480, 600, 720, 800, 820};

    @SuppressLint("DefaultLocale")
    public static void gen() {
        String base = "BasicLibrary";

        File file = new File("./" + base + "/src/main/res/values/dimens.xml");
        BufferedReader reader = null;
        ArrayList<StringBuilder> sws = new ArrayList<>();

        try {
            System.out.println("生成不同分辨率：");
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束

            for (int dpi : dpis) {
                StringBuilder builder = new StringBuilder();
                sws.add(builder);
            }
            while ((tempString = reader.readLine()) != null) {
                if (tempString.contains("</dimen>")) {
                    //tempString = tempString.replaceAll(" ", "");
                    String start = tempString.substring(0, tempString.indexOf(">") + 1);
                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);
                    float num = Float.parseFloat(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));

                    for (int i = 0; i < sws.size(); i++) {
//                        sws.get(i).append(start).append(Math.round(num * dpis[i] / defaultDp)).append(end).append("\n");
                        float f = num * (1 + (dpis[i] - defaultDp) / 1200f);
                        sws.get(i).append(start).append(Math.abs(num) < 2 ? String.format("%.1f", Double.valueOf(String.valueOf(f))) : Math.round(f)).append(end).append("\n");
                    }
                } else {
                    for (StringBuilder sw : sws) {
                        sw.append(tempString).append("\n");
                    }
                }
                line++;
            }
            reader.close();

            for (int i = 0; i < sws.size(); i++) {
                String swfile = "./" + base + "/src/main/res/values-sw" + dpis[i] + "dp/dimens.xml";
                System.out.println(sws.get(i).toString());
                writeFile(swfile, sws.get(i).toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFile(String file, String text) {
        PrintWriter out = null;
        try {
            System.out.println(file);
            if (!new File(file).exists()) {
                new File(file).getParentFile().mkdirs();
                new File(file).createNewFile();
            }
            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }

        out.close();
    }

    public static void main(String[] args) {
        gen();
    }
}
